package myky.com.doctorapp.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_consult_details.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.ConsultDcotorRepeatPhotoAdapter
import myky.com.doctorapp.adapter.ConsultPatintionPhotoAdapter
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.ConsultDetailsContract
import myky.com.doctorapp.entity.ConsultDetailsEntity
import myky.com.doctorapp.entity.ImageParamBean
import myky.com.doctorapp.presenter.ConsultDetailsPresenter
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.RecyclerUtils
import myky.com.doctorapp.utils.SystemUtil
import myky.com.doctorapp.utils.ToastUtils


/**
 * Description: 咨询详情
 * @author xiangyao
 * Created by 2019/3/11 11:41 AM
 */
class ConsultDetailsActivity : BaseActivity<ConsultDetailsPresenter, ConsultDetailsEntity>(), ConsultDetailsContract.View, BaseQuickAdapter.OnItemChildClickListener {
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        when(view?.id){

            R.id.item_consult_details_iv->{
                ToastUtils.show("患者上传的第${position+1}张图片")
            }
            R.id.doctor_repeat_pic->{
                ToastUtils.show("专家回复的第${position+1}张图片")

            }

        }
    }


    var consultId: Int = -1
    var customIsExpend: Boolean = false
    var replyIsExpend: Boolean = false
    var showCustomPhotos: List<ImageParamBean> = ArrayList(16)
    var expandCustomPhotos: List<ImageParamBean> = ArrayList<ImageParamBean>(16)
    var showReplyPhotos: List<String> = ArrayList(16)
    var expandReplyPhotos: List<String> = ArrayList(16)
    val customBeans = ArrayList<ImageParamBean>(16)
    val replyBeans = ArrayList<String>(16)
    val imageParamBeans_all = ArrayList<ImageParamBean>()
    lateinit var consultPatintionPhotoAdapter: ConsultPatintionPhotoAdapter
    lateinit var consultDoctorRepeatPhotoAdapte: ConsultDcotorRepeatPhotoAdapter
    var doctorRepeatAllPhoto = ArrayList<String>()
    var doctorRepeatPhoto = ArrayList<String>()

    lateinit var mainDoctor: ConsultDetailsEntity.SpecialistConsultBean.DoctorBean

    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.injectView)

    override fun initData() {

        mPresenter.findConsultDeatails(consultId)


    }

    override fun initView() {
        val extras = intent.extras

        if (extras != null) {

            consultId = extras.getInt(Constants.CONSULTID)

        }


        backLayout.setOnClickListener {
            finish()
        }


        RecyclerUtils.setRecyclerGridLayout(this, activity_new_consult_details_all_photo, 3)
        RecyclerUtils.setRecyclerGridLayout(this, act_new_consult_details_all_replyphoto, 3)


        conmonTitleTextView.text = "咨询详情"


        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.conmonLayout)
                .init()

        consultPatintionPhotoAdapter = ConsultPatintionPhotoAdapter(customBeans)
        activity_new_consult_details_all_photo.layoutManager = GridLayoutManager(this, 3)
        activity_new_consult_details_all_photo.adapter = consultPatintionPhotoAdapter


        consultDoctorRepeatPhotoAdapte = ConsultDcotorRepeatPhotoAdapter(doctorRepeatPhoto)
        act_new_consult_details_all_replyphoto.layoutManager = GridLayoutManager(this, 3)
        act_new_consult_details_all_replyphoto.adapter = consultDoctorRepeatPhotoAdapte


        activity_new_consult_details_replymorePhoto.setOnClickListener {
            if (replyIsExpend) {
                replyIsExpend = false
                replyBeans.clear()
                replyBeans.addAll(showReplyPhotos)
                consultDoctorRepeatPhotoAdapte.notifyDataSetChanged()
                val drawable = resources.getDrawable(R.drawable.more_icon)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight) //设置边界
                activity_new_consult_details_replymorePhoto.setCompoundDrawables(null, null, drawable, null)//画在右边
            } else {
                replyIsExpend = true
                replyBeans.clear()
                replyBeans.addAll(expandReplyPhotos)
                consultDoctorRepeatPhotoAdapte.notifyDataSetChanged()
                val drawable = resources.getDrawable(R.drawable.shouqi_icon)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight) //设置边界
                activity_new_consult_details_replymorePhoto.setCompoundDrawables(null, null, drawable, null)//画在右边
            }


        }




        activity_new_consult_details_morePhoto.setOnClickListener {
            if (customIsExpend) {

                customIsExpend = false
                customBeans.clear()
                customBeans.addAll(showCustomPhotos)
                consultPatintionPhotoAdapter.notifyDataSetChanged()
                val drawable = resources.getDrawable(R.drawable.more_icon)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight) //设置边界
                activity_new_consult_details_morePhoto.setCompoundDrawables(null, null, drawable, null)//画在右边
                activity_new_consult_details_morePhoto.text = "展开全部图片"
            } else {
                customIsExpend = true
                customBeans.clear()
                customBeans.addAll(expandCustomPhotos)
                consultPatintionPhotoAdapter.notifyDataSetChanged()
                val drawable = resources.getDrawable(R.drawable.shouqi_icon)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight) //设置边界
                activity_new_consult_details_morePhoto.setCompoundDrawables(null, null, drawable, null)//画在右边
                activity_new_consult_details_morePhoto.text = "收起部分图片"
            }
        }


        activity_new_consult_details_doctor_all.setOnClickListener {

            val type = mainDoctor.type

            when (type) {
                0 -> {

                    var bundle = Bundle()
                    bundle.putInt(Constants.DOCTORID, mainDoctor.doctorid)

                    startActivity(DoctorDetailsActivity::class.java, bundle)
                }

                1 -> {
                    val intent = Intent()
//                    intent.setClass(this, WebDoctorTeamActivity::class.java!!)
//                    intent.putExtra("INTENTTYPE", 1)
//                    intent.putExtra("fromType", Integer.valueOf(mainDoctor.doctorid))
//                    startActivity(intent)}
                }


            }
        }
        consultPatintionPhotoAdapter.onItemChildClickListener = this
        consultDoctorRepeatPhotoAdapte.onItemChildClickListener = this

    }


    override fun getContenView(): Int = R.layout.activity_consult_details

    override fun showDataSuccess(data: ConsultDetailsEntity?) {

        activity_new_consult_details_content.text = data?.specialistConsult?.content

        activity_new_consult_details_date.text = data?.specialistConsult?.createtime


        val state = data?.specialistConsult?.consultStatus
        if (state == 2) {
            activity_new_consult_details_doctor_state.text = "已回复"
            activity_new_consult_details_doctor_state.setTextColor(resources.getColor(R.color.theme_app))
            act_new_consult_reply_doctor_rl.visibility = View.VISIBLE

        } else if (state == 1 || state == 0) {
            activity_new_consult_details_doctor_state.text = "待回复"
            activity_new_consult_details_doctor_state.setTextColor(resources.getColor(R.color.yellow_f5a628))
            act_new_consult_reply_doctor_rl.visibility = View.GONE
        } else if (state == 3) {
            activity_new_consult_details_doctor_state.text = "已取消"
            activity_new_consult_details_doctor_state.setTextColor(resources.getColor(R.color.gray_999999))
            act_new_consult_reply_doctor_rl.visibility = View.GONE
            act_new_result_refusal_resaon_rl.visibility = View.VISIBLE


        }

        val sex = data?.specialistConsult?.customer?.gender
        when (sex) {
            0 -> activity_new_consult_details_patients_sex.text = "男"
            1 -> activity_new_consult_details_patients_sex.text = "女"
        }
        activity_new_consult_details_patients_age.text = data?.specialistConsult?.customer?.age.toString().plus(" 岁")
        activity_new_consult_details_patients_name.text = data?.specialistConsult?.customer?.realname


    }


    /**
     * @param doctor 咨询医生的信息
     * */
    override fun showDoctorInfo(doctor: ConsultDetailsEntity.SpecialistConsultBean.DoctorBean?) {
        if (doctor != null) {
            mainDoctor = doctor

            activity_new_consult_details_doctor_iv.setImageURI(Address.IMAGE + doctor.handPhone)

            activity_new_consult_details_doctor_name.text = doctor.realname

            activity_new_consult_details_doctor_title.text = doctor.doctoradministration

            activity_new_consult_details_doctor_departments.text = doctor.departmentname


            val doctotType = doctor.type


            when (doctotType) {
                0 -> {
                    activity_new_consult_details_team_name.visibility = View.GONE
                    activity_new_consult_details_team_title.visibility = View.GONE
                }
                1 -> {
                    activity_new_consult_details_team_name.visibility = View.VISIBLE
                    activity_new_consult_details_team_title.visibility = View.VISIBLE
                    activity_new_consult_details_team_name.text = doctor.allname
                }
            }
        }
    }

    /**
     *
     * @param doctor 咨询回复的医生信息
     *
     * **/
    override fun showRepeatDoctorInfo(doctor: ConsultDetailsEntity.SpecialistConsultBean.AnswerListBean.DoctorBean?) {

        repeatdoctorheader.setImageURI(Address.IMAGE + doctor?.handPhone)

        activity_new_consult_details_replydoctor_name.text = doctor?.realname

        act_new_consult_details_replydoctor_departments
                .text = doctor?.departmentname
        act_new_consult_details_replydoctor_title.text = doctor?.doctoradministration

        when (doctor?.type) {
            0 -> {
                act_new_consult_details_reply_team_name.visibility = View.GONE
            }
            1 -> {
                act_new_consult_details_reply_team_name.visibility = View.VISIBLE
                act_new_consult_details_reply_team_name.text = doctor.allname
            }
        }


    }

    /**
     * 专家回复的具体内容
     * **/
    override fun showDoctorRepeatContent(answerListBean: ConsultDetailsEntity.SpecialistConsultBean.AnswerListBean?) {

        activity_new_consult_details_replydate.text = answerListBean?.createtime

        act_new_consult_details_replycontent.text = answerListBean?.text

        act_new_result_refusal_resaon.text = answerListBean?.text

        val resList = answerListBean?.resList

        if (resList != null) {
            if (resList.size > 6) {
                activity_new_consult_details_replymorePhoto.visibility = View.VISIBLE

                val subList = resList.subList(0, 6)

                doctorRepeatPhoto.addAll(subList)

                doctorRepeatAllPhoto.addAll(resList.subList(0, resList.size))
            } else {
                activity_new_consult_details_replymorePhoto.visibility = View.GONE
                doctorRepeatPhoto.addAll(resList.subList(0, resList.size))
            }
            replyBeans.addAll(showReplyPhotos)
            consultDoctorRepeatPhotoAdapte.notifyDataSetChanged()
        } else {
            activity_new_consult_details_replymorePhoto.visibility = View.GONE
        }

    }

    /**
     * @param consultDisease 患者咨询的疾病
     * */
    override fun showConsultDisease(consultDisease: ConsultDetailsEntity.SpecialistConsultBean.ConsultDiseaseBean?) {


        if (consultDisease != null) {

            val casePictures = consultDisease.casePictures
            val caseForms = consultDisease.caseForms

            if (caseForms != null) {
                for (i in caseForms.indices) {
                    imageParamBeans_all.add(ImageParamBean(SystemUtil.getTextDate(caseForms[i].createtime), caseForms[i].caseTemplateName, caseForms[i].caseTemplateId, caseForms[i].htmlDocId))
                }
            }
            if (casePictures != null) {
                for (x in casePictures.indices) {
                    imageParamBeans_all.add(ImageParamBean(Address.IMAGE + casePictures[x].pictureMD5, SystemUtil.getTextDate(casePictures[x].createtime), casePictures.get(x).caseTemplateName, casePictures[x].id))
                }
            }

            if (imageParamBeans_all.size > 6) {
                activity_new_consult_details_morePhoto.visibility = View.VISIBLE
                showCustomPhotos = imageParamBeans_all.subList(0, 6)
                expandCustomPhotos = imageParamBeans_all.subList(0, imageParamBeans_all.size)
            } else {
                activity_new_consult_details_morePhoto.visibility = View.GONE
                showCustomPhotos = imageParamBeans_all.subList(0, imageParamBeans_all.size)
            }
            customBeans.addAll(showCustomPhotos)
            consultPatintionPhotoAdapter.notifyDataSetChanged()

        }
    }

    override fun getPresenter(): ConsultDetailsPresenter {
        return ConsultDetailsPresenter()
    }

}
